from mulib import caps, mu
from eventlet import api, httpd
from mulib.mud import MethodGuard

class CapsServer(object):
    """a caps server"""
    
    def __init__(self,
        private_ip="127.0.0.1", 
        private_port=8080, 
        public_ip="0.0.0.0",
        public_port=8080,
        public_prefix="cap",
        private_prefix="_cap"):
        """initialize the caps server"""
        
        self.private_ip = private_ip
        self.private_port = private_port
        self.public_ip= public_ip
        self.public_port = public_port
        self.public_prefix = public_prefix
        self.private_prefix = private_prefix
        
    def run(self):
        """start the server"""
        root = {}

        site = mu.SiteMap(root)

        site.root[self.private_prefix] = cap_store = caps.CapabilityStore(
            'http://localhost:%s/cap/' % self.private_port)
        site.root[self.public_prefix] = caps.CapabilityProxy(cap_store)

        api.spawn(
            httpd.server,
            api.tcp_listener((self.private_ip, self.private_port)),
            site,
            max_size=512)

        httpd.server(
            api.tcp_listener((self.public_ip, self.public_port)),
            mu.SiteMap(MethodGuard(site.root, GET=True)),
            max_size=512)

def run_default_server():
    
    server=CapsServer()
    server.run()